<template>
  <div class="contact_box" v-if="is_show_selected">
    <div class="title">
      <van-nav-bar
        :title="'已选择' + selected_list.length + '人'"
        left-text="返回"
        left-arrow
        @click-left="hide"
      >
        <van-button type="info" size="small" slot="right" @click="confirm">确定</van-button>
      </van-nav-bar>
    </div>
    <div class="list_box">
      <div v-for="item in selected_list" :key="item.id" class="item" >
        <van-image
          v-show="item.avatar"
          round
          width="40px"
          height="40px"
          :src="item.avatar"
          style="margin-top: 5px"
        />
        <div class="image" v-show="!item.avatar">
          {{item.alias.substring(item.alias.length-2)}}
        </div>
        <div class="name_box van-hairline--bottom">
          {{item.alias}}
          <span class="remove" @click="removeContact(item)">
            移除
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      is_show_selected: false,
      selected_list:[],
    }
  },
  methods:{
    // 确认
    confirm(){
      this.$emit('selectedList',this.selected_list)
      this.hide()
    },
    // 显示
    show(list){
      if(list){
        this.selected_list = []
        list.forEach(item => {
           this.selected_list.push(item)
        })
      }
      this.is_show_selected = true
    },
    // 关闭
    hide(){
      this.is_show_selected = false
      this.$emit('hide',false)
    },
    // 移除
    removeContact(contact){
      this.selected_list.forEach((item,index) => {
        if (contact.id == item.id){
          this.selected_list.splice(index,1);
        }
      });
    }
  }
}
</script>

<style lang="less" scoped>
.contact_box{
  position: absolute;
  top: 200px;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 99;
  .title{
    position: absolute;
    height: 46px;
    top: 0;
    width: 100%;
    background-color: #fff;
    border-radius: 15px 15px 0  0 !important;
    overflow: hidden !important;
  }
  .list_box{
    background-color: #fff;
    position: absolute;
    top: 46px;
    bottom: 0px;
    width: 100%;
    overflow: auto;
    .item{
      height: 50px;
      padding: 0px 15px;
      line-height: 50px;
      box-sizing: border-box;
      display: flex;
      .image{
        width: 40px;
        height: 40px;
        background-color: rgb(58, 148, 250);
        margin-top: 5px;
        color: #fff;
        text-align: center;
        line-height: 40px;
        overflow: hidden;
        border-radius: 50%;
        font-size: 14px;
      }
      .name_box{
        flex: 1;
        font-size: 14px;
        font-weight: 500;
        line-height: 50px;
        margin-left: 15px;
        position: relative;
        .remove{
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          right: 0;
          color: rgb(233, 98, 83);
        }
      }
    }
  }
}
</style>